﻿using AutoMapper;
using HamcoWcs.Contract.ContractCommon;
using HamcoWcs.Contract.ContractForeEnd;
using HamcoWcs.DAL;
using HamcoWcs.Entitys.DeviceMsgQueues;
using HamcoWcs.Models.Common.Enums;
using HamcoWcs.Models.DeviceMsgQueues;
using HamcoWcs.Models.DeviceMsgs;
using HamcoWcs.Models.DeviceMsgs.SrmMsgs;
using HamcoWcs.Models.ForeEndModels.DeviceMsgQueues;
using HamcoWcs.Service.Common;
using System;
using System.Linq;
using System.Threading.Tasks;

namespace HamcoWcs.Service.ForeEnd
{
    [AutoInject(typeof(IForeEndDeviceMsgQueueService), InjectType.Scope)]
    public class ForeEndDeviceMsgQueueService : ForeEndBaseService, IForeEndDeviceMsgQueueService
    {
        private readonly AutomaticStorageInfo _asrs;

        public ForeEndDeviceMsgQueueService(WcsDbContext wcsDbContext, IMapper mapper, ILoggerService logger, AutomaticStorageInfo asrs) : base(wcsDbContext, mapper, logger)
        {
            _asrs = asrs;
        }

        public void CreateNewDeviceMsg(CreateDeviceMsgQueueDto createDeviceMsgQueue)
        {
            var machineName = createDeviceMsgQueue.MachineName;
            var portName = createDeviceMsgQueue.PortName;
            var request = (ushort)createDeviceMsgQueue.Request;

            if (string.IsNullOrEmpty(machineName) || string.IsNullOrEmpty(portName))
                throw new Exception("前端发送指令时传入错误的设备名称");

            DeviceMsgQueueDto deviceMsgQueueDto;
            switch (createDeviceMsgQueue.DeviceType)
            {
                case EnumDeviceType.CV:
                    var pcon = _asrs.Pcons.FirstOrDefault(x => x.MachineName == machineName && x.PortName == portName)
                        ?? throw new Exception($"未找到设备[{machineName}-{portName}]");

                    var pconMsg = new PconSendMsg()
                    {
                        PconName = (ushort)pcon.PconName,
                        Request = request
                    };

                    deviceMsgQueueDto = new DeviceMsgQueueDto(pcon, pconMsg);
                    break;
                case EnumDeviceType.Srm:
                    var srm = _asrs.Srms.FirstOrDefault(x => x.MachineName == machineName && x.PortName == portName)
                        ?? throw new Exception($"未找到设备[{machineName}-{portName}]");

                    var srmMsg = new SrmSendMsg(false)
                    {
                        Request = (ushort)createDeviceMsgQueue.Request
                    };

                    deviceMsgQueueDto = new DeviceMsgQueueDto(srm, srmMsg);
                    break;
                case EnumDeviceType.Lift:
                    throw new NotImplementedException();
                case EnumDeviceType.Rgv:
                    throw new NotImplementedException();
                default:
                    throw new Exception("传入的设备类型错误");
            }

            var deviceMsgQueue = _mapper.Map<DeviceMsgQueue>(deviceMsgQueueDto);
            _wcsDbContext.Add(deviceMsgQueue);
             Save();
        }
    }
}
